package com.smart.store.service.impl;

import com.smart.store.enums.PriorityEnum;
import com.smart.store.model.entity.WishEntity;
import com.smart.store.model.request.WishDto;
import com.smart.store.model.response.WishResponse;
import com.smart.store.repository.WishRepository;
import com.smart.store.service.WishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class WishServiceImpl implements WishService {

    @Autowired
    WishRepository wishRepository;

    @Override
    public void completeWish(String wishId) {
        WishEntity wishEntity = wishRepository.getByWishId(wishId);
        wishEntity.setStatus(2);
        wishEntity.setCompleteDate(LocalDate.now());
        wishRepository.save(wishEntity);
    }

    @Override
    public void deleteWish(String wishId) {
        WishEntity wishEntity = wishRepository.getByWishId(wishId);
        wishEntity.setStatus(3);
        wishRepository.save(wishEntity);
    }

    @Override
    public void abortWish(String wishId) {
        WishEntity wishEntity = wishRepository.getByWishId(wishId);
        wishEntity.setStatus(0);
        wishRepository.save(wishEntity);
    }

    @Override
    public List<WishResponse> getWish() {
        List<WishResponse> responses = new ArrayList<>();
        List<WishEntity> wishEntityList = wishRepository.getWishNotComplete();
        if (wishEntityList != null) {
            for (WishEntity wishEntity : wishEntityList) {
                WishResponse response = entityToResponse(wishEntity);
                responses.add(response);
            }
        }
        return responses;
    }

    private WishResponse entityToResponse(WishEntity wishEntity) {
        WishResponse wishResponse = new WishResponse();
        wishResponse.setComment(wishEntity.getComment());
        wishResponse.setName(wishEntity.getWishName());
        wishResponse.setPriority(wishEntity.getPriority());
        wishResponse.setWishId(wishEntity.getWishId());
        wishResponse.setStatus(wishEntity.getStatus());
        wishResponse.setCompleteDate(wishEntity.getCompleteDate());
        return wishResponse;
    }

    @Override
    public void makeWish(WishDto wishDto) {
        WishEntity wishEntity = new WishEntity();
        wishEntity.setStatus(0);
        wishEntity.setComment(wishDto.getComment());
        wishEntity.setPriority(wishDto.getPriority());
        wishEntity.setWishDate(LocalDate.now());
        wishEntity.setWishName(wishDto.getName());
        wishEntity.setWishId(UUID.randomUUID().toString());
        wishRepository.save(wishEntity);
    }

    @Override
    public void updateWish(WishDto wishDto) {
        WishEntity wishEntity = wishRepository.getByWishId(wishDto.getWishId());
        wishEntity.setComment(wishDto.getComment());
        wishEntity.setPriority(wishDto.getPriority());
        wishEntity.setWishDate(LocalDate.now());
        wishEntity.setWishName(wishDto.getName());
        wishRepository.save(wishEntity);
    }

    @Override
    public List<WishResponse> getCompleteWish() {
        List<WishResponse> responses = new ArrayList<>();
        List<WishEntity> wishEntityList = wishRepository.getAllByStatus(2);
        if (wishEntityList != null) {
            for (WishEntity wishEntity : wishEntityList) {
                WishResponse wishResponse = entityToResponse(wishEntity);
                responses.add(wishResponse);
            }
        }
        return responses;
    }
}
